package com.qijian.flowable.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qijian.common.annotation.Log;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.core.page.TableDataInfo;
import com.qijian.common.enums.BusinessType;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.qijian.common.utils.poi.ExcelUtil;
import com.qijian.flowable.domain.FlowableForm;
import com.qijian.flowable.domain.query.FlowableFormQuery;
import com.qijian.flowable.service.IFlowableFormService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 流程表单Controller
 *
 * @author qijian
 * @date 2021-09-17
 */
@Api(tags = "流程模块：流程表单")
@RestController
@RequiredArgsConstructor
@RequestMapping("/flowable/flowableForm")
public class FlowableFormController extends BaseController {

    private final IFlowableFormService flowableFormService;

    /**
     * 查询流程表单列表
     */
    @PreAuthorize("@ss.hasPermi('flowable:flowableForm:list')")
    @GetMapping("/list")
    public TableDataInfo list(FlowableFormQuery query, IPage<FlowableForm> page) {
        return getDataTable(flowableFormService.page(Page.of(page.getCurrent(), page.getSize()), WhereEntityTool.invoke(query)));
    }

    /**
     * 导出流程表单列表
     */
    @PreAuthorize("@ss.hasPermi('flowable:flowableForm:export')")
    @Log(title = "流程表单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FlowableFormQuery query) {
        return new ExcelUtil<>(FlowableForm.class).exportExcel(flowableFormService.list(WhereEntityTool.invoke(query)), "流程表单数据");
    }

    /**
     * 获取流程表单详细信息
     */
    @GetMapping(value = "/{formKey}")
    public AjaxResult getInfo(@PathVariable("formKey") String formKey) {
        return AjaxResult.success(flowableFormService.getById(formKey));
    }

    /**
     * 新增流程表单
     */
    @PreAuthorize("@ss.hasPermi('flowable:flowableForm:add')")
    @Log(title = "流程表单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FlowableForm flowableForm) {
        FlowableFormQuery query = new FlowableFormQuery();
        query.setFormType(flowableForm.getFormType());
        long count = flowableFormService.count(WhereEntityTool.invoke(query));
        flowableForm.setFormKey(flowableForm.getFormType() + (count > 0 ? count : ""));
        return toAjax(flowableFormService.save(flowableForm));
    }

    /**
     * 修改流程表单
     */
    @PreAuthorize("@ss.hasPermi('flowable:flowableForm:edit')")
    @Log(title = "流程表单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FlowableForm flowableForm) {
        return toAjax(flowableFormService.updateById(flowableForm));
    }

    /**
     * 删除流程表单
     */
    @PreAuthorize("@ss.hasPermi('flowable:flowableForm:remove')")
    @Log(title = "流程表单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{formKeys}")
    public AjaxResult remove(@PathVariable List<String> formKeys) {
        return toAjax(flowableFormService.removeByIds(formKeys));
    }
}
